package com.windoent.yugioh.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.windoent.yugioh.R;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.TDevice;
import com.windoent.yugioh.utils.ToastUtils;

/**
 * 作者：Android
 * 时间：2021/6/16 11:08
 * 功能：退赛理由
 */
public class ExitGameReasonDialog extends Dialog {
    private TextView mTitle;
    private TextView mNo;
    private TextView mYes;
    private EditText mEtContent;

    private onClickListener listener;
    private String titleStr;

    public void setOnclickListener(onClickListener clickListener) {
        this.listener = clickListener;
    }

    public ExitGameReasonDialog(Context context) {
        super(context, R.style.MoHUDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_no_title_exit_game_reason_dialog_layout);
        //按空白处能取消动画
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(attributes);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        mYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String editStr = mEtContent.getText().toString().trim();
                if (BaseMethod.notNull(editStr) && editStr.length() < 10) {
                    ToastUtils.showText(getContext().getString(R.string.string_name_length_limit_less_ten));
                } else if (BaseMethod.notNull(editStr) && editStr.length() >= 50) {
                    ToastUtils.showText(getContext().getString(R.string.string_name_length_limit_more_fiveten));
                } else if (BaseMethod.notNull(editStr)) {
                    if (listener != null) {
                        listener.onClick(editStr);
                        dismiss();
                    }
                } else {
                    ToastUtils.showText(getContext().getString(R.string.string_name_length_limit_less_ten));
                }
            }
        });
        mNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        mTitle.setText(BaseMethod.notNullDefaultValue(titleStr, getContext().getString(R.string.string_exit_games_reason_title)));
    }

    /**
     * 初始化界面控件
     */
    private void initView() {

        mTitle = (TextView) findViewById(R.id.title);
        mNo = (TextView) findViewById(R.id.no);
        mYes = (TextView) findViewById(R.id.yes);
        mEtContent = (EditText) findViewById(R.id.et_content);

    }

    public void setTitle(String s) {
        titleStr = s;
    }

    public interface onClickListener {
        public void onClick(String string);
    }
}
